package com.zsc.boot.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zsc.boot.bean.Team;
import com.zsc.boot.bean.User;
import com.zsc.boot.bean.dto.TeamQuery;
import com.zsc.boot.bean.request.TeamAddRequest;
import com.zsc.boot.bean.vo.TeamUserVO;
import com.zsc.boot.common.BaseResponse;
import com.zsc.boot.common.ErrorType;
import com.zsc.boot.common.ResultResponse;
import com.zsc.boot.exception.BusinessException;
import com.zsc.boot.service.TeamService;
import com.zsc.boot.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/team")
@CrossOrigin(origins = {"http://localhost:5173"},allowCredentials = "true")
@Slf4j
public class TeamController {

    @Resource
    private TeamService teamService;

    @Resource
    private UserService userService;

    /**
     * 创建（添加）队伍
     * @param teamAddRequest
     * @return
     */
    @PostMapping("/add")
    public BaseResponse<Long> addTeam(@RequestBody TeamAddRequest teamAddRequest, HttpServletRequest request){
        if(teamAddRequest == null){
            throw new BusinessException(ErrorType.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        Team team = new Team();

        //将使用apache工具将teamAddRequest传给team
        try {
            BeanUtils.copyProperties(team,teamAddRequest);
        } catch (Exception e) {
            e.printStackTrace();
        }

        //调用业务逻辑层的方法进行校验与插入
        long teamId = teamService.createTeam(team,loginUser);
        if(teamId == 0){
            throw new BusinessException(ErrorType.NULL_ERROR);
        }
        //因为要返回的是添加成功数目，这里mybatis添加成功后会在属性上生成一个id
        return ResultResponse.success(team.getId());
    }

    /**
     * 根据id删除队伍
     * @param id
     * @return
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteTeam(long id){
        if(id <= 0){
            throw new BusinessException(ErrorType.PARAMS_ERROR);
        }
        boolean result = teamService.removeById(id);
        if(!result){
            throw new BusinessException(ErrorType.NULL_ERROR);
        }
        return ResultResponse.success(result);
    }

    /**
     * 更新队伍
     * @param team
     * @return
     */
    @PostMapping("/update")
    public BaseResponse<Boolean> updateTeam(@RequestBody Team team){
        if(team == null){
            throw new BusinessException(ErrorType.PARAMS_ERROR);
        }
        boolean result = teamService.updateById(team);
        if(!result){
            throw new BusinessException(ErrorType.NULL_ERROR);
        }
        return ResultResponse.success(result);
    }

    /**
     * 根据id查询队伍
     * @param id
     * @return
     */
    @GetMapping("/get")
    public BaseResponse<Team> updateTeam(Long id){
        if(id <= 0){
            throw new BusinessException(ErrorType.PARAMS_ERROR);
        }
        Team team = teamService.getById(id);
        if(team == null){
            throw new BusinessException(ErrorType.NULL_ERROR);
        }
        return ResultResponse.success(team);
    }

    /**
     * 查询所有队伍
     * @param teamQuery：请求参数包装类
     * @return
     */
    @GetMapping("/list")
    public BaseResponse<List<TeamUserVO>> getTeamList(TeamQuery teamQuery){
        if(teamQuery == null){
            throw new BusinessException(ErrorType.PARAMS_ERROR);
        }

        //team转化为teamQuery的操作，这里使用了apache的一个工具类进行转换
        Team team = new Team();
        try {
            BeanUtils.copyProperties(team,teamQuery);
        } catch (Exception e) {
            throw new BusinessException(ErrorType.SYSTEM_ERROR);
        }
        //使用teamService.list查询是发现接收的参数只能是team，所以要将team强转为teamQuery
        QueryWrapper<Team> queryWrapper = new QueryWrapper<>(team);
        List<Team> list = teamService.listTeam(teamQuery);
        return ResultResponse.success(list);
    }
//
//    @GetMapping("/list")
//    public BaseResponse<List<Team>> getTeamList(TeamQuery teamQuery){
//        if(teamQuery == null){
//            throw new BusinessException(ErrorType.PARAMS_ERROR);
//        }
//
//        //team转化为teamQuery的操作，这里使用了apache的一个工具类进行转换
//        Team team = new Team();
//        try {
//            BeanUtils.copyProperties(team,teamQuery);
//        } catch (Exception e) {
//            throw new BusinessException(ErrorType.SYSTEM_ERROR);
//        }
//        //使用teamService.list查询是发现接收的参数只能是team，所以要将team强转为teamQuery
//        QueryWrapper<Team> queryWrapper = new QueryWrapper<>(team);
//        List<Team> list = teamService.list(queryWrapper);
//        return ResultResponse.success(list);
//    }


    /**
     * 分页查询队伍
     * @param teamQuery：请求参数包装类
     * @return
     */
    @GetMapping("/list/page")
    public BaseResponse<Page<Team>> getTeamListPage(TeamQuery teamQuery){
        if(teamQuery == null){
            throw new BusinessException(ErrorType.PARAMS_ERROR);
        }

        //team转化为teamQuery的操作，这里使用了apache的一个工具类进行转换
        Team team = new Team();
        try {
            BeanUtils.copyProperties(team,teamQuery);
        } catch (Exception e) {
            throw new BusinessException(ErrorType.SYSTEM_ERROR);
        }
        //使用teamService.list查询是发现接收的参数只能是team，所以要将team强转为teamQuery
        QueryWrapper<Team> queryWrapper = new QueryWrapper<>(team);

        Page<Team> page = new Page<>(teamQuery.getPageNum(),teamQuery.getPageSize());
        Page<Team> teamPage = teamService.page(page, queryWrapper);
        return ResultResponse.success(teamPage);
    }
    

}

